import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/views/index.vue'
import Connected from '@/views/connected.vue'
import Test from '@/views/tabtest.vue'
import EditAudio from '@/views/edit-audio.vue'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'in',
      meta: {title: '首页'},
      component: Index
    },
    {
      path: '/index/:id?',
      name: 'index',
      meta: {title: '首页'},
      component: Index
    },
    {
      path: '/connected/:type(preview|detail)/:id',
      name: 'connected',
      component: Connected
    },
    {
      path: '/test',
      name: 'test',
      component: Test
    },
    {
      path: '/edit-audio/:id?',
      name: 'edit-audio',
      meta: {title: '换音'},
      component: EditAudio
    }, {
      path: '/scan',
      meta: {title: '关注公众号'},
      component: () => import('../views/Scan')
    }, {
      path: '/home/:id',
      meta: {title: '我也要玩'},
      component: () => import('../views/Home')
    }
  ]
})

const iframeLoad = function (src) {
  let iframe = document.createElement('iframe')
  iframe.style.display = 'none'
  iframe.src = src
  document.body.appendChild(iframe)
  iframe.addEventListener('load', function () {
    setTimeout(function () {
      iframe.remove()
    }, 0);
  })
};

router.beforeEach((to, from, next) => {
  next()
})

router.afterEach(route => {
  document.title = route.meta.title || ''
  if (process.env.NODE_ENV === 'development') {
    return route
  }
  if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
    let src = '/static/fixrouter.html?' + Math.random()
    iframeLoad(src)
  }
})

export default router
